package com.runfast.waimai.web.controller.business;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastCityService;
import com.runfast.waimai.service.RunfastProvinceService;
import com.runfast.waimai.service.RunfastRegionService;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author chenzheng
 * @create 2018-05-17 16:11
 **/

@RestController("provinceCityDistrictController")
@RequestMapping("/server/provinceCityDistrict")
public class ProvinceCityDistrictController {
    @Autowired
    private RunfastCityService runfastCityService;
    @Autowired
    private RunfastProvinceService runfastProvinceService;
    @Autowired
    private RunfastRegionService runfastRegionService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    @GetMapping("/findProvinceCityDistrict")
    public Result findProvinceCityDistrict(Integer agentid){
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        RunfastBusinessAccounts accounts = SessionContext.getRunfastBusinessAccounts();
        List<RunfastProvince> provinces=null;
        if(admin!=null && admin.getType()==2 && agentAccounts!=null ){

            /*if(agentAccounts.getPid()!=null && agentAccounts.getCid()!=null&&agentAccounts.getTid()!=null){
                RunfastProvinceExample provinceExample=new RunfastProvinceExample();
                provinceExample.createCriteria().andIdEqualTo(agentAccounts.getPid());
                provinces=runfastProvinceService.selectByExample(provinceExample);
                if(provinces.size()>0){
                    for(RunfastProvince province:provinces){
                        RunfastCityExample example=new RunfastCityExample();
                        example.createCriteria().andIdEqualTo(agentAccounts.getCid());
                        List<RunfastCity> list=runfastCityService.selectByExample(example);
                        if(list.size()>0){
                            for(RunfastCity city:list){
                                agentAccounts.getAgents();
                                List<Integer> agents=agentAccounts.getAgents();//代理商所管的代理商ids
                                WaiMaiRunfastAgentbusinessExample example1=new WaiMaiRunfastAgentbusinessExample();
                                example1.or().andIdEqualTo(agentAccounts.getAgentid());
                                example1.createCriteria().andAgentidIn(agents);
                                List<WaiMaiRunfastAgentbusiness> list1 = waiMaiRunfastAgentbusinessService.selectByExample(example1);
                                if(list1.size()>0){
                                    List<RunfastRegion> regionList=new ArrayList<RunfastRegion>();
                                    for(WaiMaiRunfastAgentbusiness agentbusiness:list1){
                                        RunfastRegion region=runfastRegionService.selectByPrimaryKey(agentbusiness.getTid());
                                        regionList.add(region);
                                    }
                                    city.setRegionList(regionList);
                                }
                            }
                        }
                        province.setCityList(list);
                    }
                }
            }else if(agentAccounts.getPid()!=null && agentAccounts.getCid()!=null &&agentAccounts.getTid()==null){
                RunfastProvinceExample provinceExample=new RunfastProvinceExample();
                provinceExample.createCriteria().andIdEqualTo(Integer.valueOf(agentAccounts.getPid()));
                provinces=runfastProvinceService.selectByExample(provinceExample);
                if(provinces.size()>0){
                    for(RunfastProvince province:provinces){
                        RunfastCityExample example=new RunfastCityExample();
                        example.createCriteria().andProvinceidEqualTo(province.getId());
                        List<RunfastCity> list=runfastCityService.selectByExample(example);
                        province.setCityList(list);
                    }
                }
            }*/
            provinces=selectProvinceCityDistrictByAgentId(provinces,agentAccounts.getAgentid());
        }else if(admin!=null && admin.getType()==1){
            //后台登录
            provinces=selectProvinceCityDistrictByAgentId(provinces,agentid);
            /*provinces=runfastProvinceService.selectByExample(null);
            if(provinces.size()>0){
                for(RunfastProvince province:provinces){
                    RunfastCityExample example=new RunfastCityExample();
                    example.createCriteria().andProvinceidEqualTo(province.getId());
                    List<RunfastCity> list=runfastCityService.selectByExample(example);
                    if(list.size()>0){
                        for(RunfastCity city:list){
                            RunfastRegionExample regionExample=new RunfastRegionExample();
                            regionExample.createCriteria().andCityidEqualTo(city.getId());
                            List<RunfastRegion> regionList=runfastRegionService.selectByExample(regionExample);
                            city.setRegionList(regionList);
                        }
                    }
                    province.setCityList(list);
                }
            }*/
        }
       return  Result.ok(null,provinces);
    }

    /**根据选择的代理商获取联动的省市区*/
    @GetMapping("/findProvinceCityDistrictByAgentId")
    public Result findProvinceCityDistrictByAgentId(Integer agentId){
        List<RunfastProvince> provinces=null;
        provinces=selectProvinceCityDistrictByAgentId(provinces,agentId);
        return Result.ok(null,provinces);
    }


    /*根据选择的代理商获取联动的省市区--方法*/
    public List<RunfastProvince> selectProvinceCityDistrictByAgentId(List<RunfastProvince> provinces,Integer agentId){
        if(agentId!=null){
            WaiMaiRunfastAgentbusiness business=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(agentId);
            if(business!=null){
                if(business.getPid()!=null && business.getCid()!=null&&business.getTid()!=null){
                    RunfastProvinceExample provinceExample=new RunfastProvinceExample();
                    provinceExample.createCriteria().andIdEqualTo(business.getPid());
                    provinces=runfastProvinceService.selectByExample(provinceExample);
                    if(provinces.size()>0){
                        for(RunfastProvince province:provinces){
                            RunfastCityExample example=new RunfastCityExample();
                            example.createCriteria().andIdEqualTo(business.getCid());
                            List<RunfastCity> list=runfastCityService.selectByExample(example);
                            if(list.size()>0){
                                for(RunfastCity city:list){
                                    List<Integer> agentIds =null;
                                    if (business != null) {
                                         agentIds = waiMaiRunfastAgentbusinessService.findByAgentId(business.getId());
                                    }
                                    //List<Integer> agents=business.getAgents();//代理商所管的代理商ids
                                    WaiMaiRunfastAgentbusinessExample example1=new WaiMaiRunfastAgentbusinessExample();
                                    example1.or().andIdEqualTo(business.getId());
                                    example1.createCriteria().andAgentidIn(agentIds);
                                    List<WaiMaiRunfastAgentbusiness> list1 = waiMaiRunfastAgentbusinessService.selectByExample(example1);
                                    if(list1.size()>0){
                                        List<RunfastRegion> regionList=new ArrayList<RunfastRegion>();
                                        for(WaiMaiRunfastAgentbusiness agentbusiness:list1){
                                            RunfastRegion region=runfastRegionService.selectByPrimaryKey(agentbusiness.getTid());
                                            regionList.add(region);
                                        }
                                        city.setRegionList(regionList);
                                    }
                                }
                            }
                            province.setCityList(list);
                        }
                    }
                }else if(business.getPid()!=null && business.getCid()!=null &&business.getTid()==null){
                    RunfastProvinceExample provinceExample=new RunfastProvinceExample();
                    provinceExample.createCriteria().andIdEqualTo(Integer.valueOf(business.getPid()));
                    provinces=runfastProvinceService.selectByExample(provinceExample);
                    if(provinces.size()>0){
                        for(RunfastProvince province:provinces){
                            RunfastCityExample example=new RunfastCityExample();
                            example.createCriteria().andIdEqualTo(business.getCid());
                            List<RunfastCity> list=runfastCityService.selectByExample(example);

                            if(list.size()>0){
                                 for(RunfastCity city:list){
                                     RunfastRegionExample example1=new RunfastRegionExample();
                                     example1.createCriteria().andCityidEqualTo(city.getId());
                                     List<RunfastRegion>  regionList=runfastRegionService.selectByExample(example1);
                                     city.setRegionList(regionList);
                                 }
                            }
                            province.setCityList(list);
                        }
                    }
                }
            }
        }else{
            //返回所有的省市区
            provinces=runfastProvinceService.selectByExample(null);
            if(provinces.size()>0){
                for(RunfastProvince province:provinces){
                    RunfastCityExample example=new RunfastCityExample();
                    example.createCriteria().andProvinceidEqualTo(province.getId());
                    List<RunfastCity> list=runfastCityService.selectByExample(example);
                    if(list.size()>0){
                        for(RunfastCity city:list){
                            RunfastRegionExample regionExample=new RunfastRegionExample();
                            regionExample.createCriteria().andCityidEqualTo(city.getId());
                            List<RunfastRegion> regionList=runfastRegionService.selectByExample(regionExample);
                            city.setRegionList(regionList);
                        }
                    }
                    province.setCityList(list);
                }
            }
        }
        return provinces;
    }

}
